package com.example.springboot.config.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * kafka的消费者
 *
 * @Author:ChenZhangKun
 * @Date: 2021/5/25 16:51
 */
//@Component
public class KafkaConsumer {
    /**
     * kafka消费者监听
     *
     * @param record
     */
    @KafkaListener(topics = "first")
    public void listen(ConsumerRecord<String, String> record) {
        // 主题
        String topic = record.topic();
        // 偏移量
        long offset = record.offset();
        // 分区
        int partition = record.partition();
        // value
        String value = record.value();
        System.out.println("topic=" + topic);
        System.out.println("offset=" + offset);
        System.out.println("partition=" + partition);
        System.out.println("value=" + value);
    }

    /**
     * 接收自己配置的主题
     *
     * @param record
     */
    @KafkaListener(topics = {"second"})
    public void listen_2(ConsumerRecord<String, String> record) {
        // 主题
        String topic = record.topic();
        // 偏移量
        long offset = record.offset();
        // 分区
        int partition = record.partition();
        // value
        String value = record.value();
        System.out.println("topic=" + topic);
        System.out.println("offset=" + offset);
        System.out.println("partition=" + partition);
        System.out.println("value=" + value);
    }

}
